package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.AdditionalDrawLotteryRuleDao;
import com.qdlc.p2p.dal.dto.AdditionalDrawLotteryRule;
import com.qdlc.p2p.dal.model.AdditionalDrawLotteryRuleModel;

/**
 * Created by liaoxiangkai on 2015/11/12.
 */
@Repository("additionalDrawLotteryRuleDao")
public class AdditionalDrawLotteryRuleDaoImpl extends BaseDaoImpl<AdditionalDrawLotteryRule> implements AdditionalDrawLotteryRuleDao {

    /**
     * 查询规则列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<AdditionalDrawLotteryRuleModel> list(AdditionalDrawLotteryRuleModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(),
                model.getRows());
        if (StringUtil.isNotBlank(model.getSearchName())) {
            param.addParam("name", SearchFilter.Operators.LIKE, model.getSearchName());
        }
        PageDataList<AdditionalDrawLotteryRule> dataList = this.findPageList(param);
        PageDataList<AdditionalDrawLotteryRuleModel> modelDataList = new PageDataList<AdditionalDrawLotteryRuleModel>();
        List<AdditionalDrawLotteryRuleModel> list = new ArrayList<AdditionalDrawLotteryRuleModel>();
        if (dataList.getList() != null && dataList.getList().size() > 0) {
            for (AdditionalDrawLotteryRule entity : dataList.getList()) {
                AdditionalDrawLotteryRuleModel additionalDrawLotteryRuleModel = AdditionalDrawLotteryRuleModel.instance(entity);
                //将修改人名字设置给model
                if (null != entity.getModifier())
                    additionalDrawLotteryRuleModel.setModifierName(entity.getModifier().getUserName());
                //把创建人名字设置给model
                if (null != entity.getCreator())
                    additionalDrawLotteryRuleModel.setCreatorName(entity.getCreator().getUserName());
                list.add(additionalDrawLotteryRuleModel);
            }
        }
        modelDataList.setPage(dataList.getPage());
        modelDataList.setList(list);
        return modelDataList;
    }

    /**
     * 更新抽奖规则状态
     *
     * @param model
     */
    @Override
    public int toggleStatus(AdditionalDrawLotteryRuleModel model) {
        String jpql = "update AdditionalDrawLotteryRule set status = ?1, modifier = ?2, modifyTime = ?3 where id = ?4";
        Query query = em.createQuery(jpql);
        query.setParameter(1, model.getStatus());
        query.setParameter(2, model.getModifier());
        query.setParameter(3, model.getModifyTime());
        query.setParameter(4, model.getId());
        return query.executeUpdate();
    }
}
